<?php
namespace Common\Model;
use Think\Log;
use Think\Model;
class LiveChannelModel extends VRLiveModel
{
	protected $autoCheckFields = false;
	
	protected $_validate = array(
		//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
		array('channelName', 'require', '频道名称不能为空！', 0, 'regex', CommonModel:: MODEL_BOTH)
	);
	
	protected $_auto = array(
			array('create_time','mGetDate',CommonModel:: MODEL_INSERT,'callback'),
	);
	
	//用于获取时间，格式为2012-02-03 12:12:12,注意,方法不能为private
	function mGetDate() {
		return date('Y-m-d H:i:s');
	}
	
	public function queryLiveChannelList($status = -1, $name = null, $pageStart, $pageSize){
		$where = array();
		if(-1 != $status){
			$where['status'] = array('EQ', $status);
		}
		if($name && !empty($name)){
			$where['channel_name'] = array('like','%'.$name.'%');
		}
		
		$list = $this->where($where)->limit($pageStart, $pageSize)->select();
		return $list;
	}
	
	public function queryLiveChannelCount($status = -1, $name = null){
		$where = array();
		if(-1 != $status){
			$where['status'] = array('EQ', $status);
		}
		if($name && !empty($name)){
			$where['channel_name'] = array('like','%'.$name.'%');
		}
		return $this->where($where)->count();
	}
	
	public function getLiveChannelList($is_fast){
		$list = $this->where(array('status'=>1,'type'=>$is_fast))->select();
		return $list;
	}

	//获取所以 无输入流的 频道
	public function getLiveChannelNoneList($is_fast,$start_time){
		//is_fast 0后台频道,1采集端频道

		$config = array('SecretId'       => C("API_SecretId"),
			'SecretKey'      => C("API_SecretKey"),
			'RequestMethod'  => 'GET',
			'DefaultRegion'  => C("API_DefaultRegion"));

		vendor("QcloudApi.QcloudApi");
		$service = \QcloudApi::load(\QcloudApi::MODULE_LIVE, $config);

		$package = array(
			'pageSize' => 100,
		);
		$a = $service->DescribeLVBChannelList($package);
		\Think\Log::record('腾讯云返回：'.json_encode($a), 'INFO');
		if($a == false){
			\Think\Log::record('腾讯云返回：'.json_encode($service->getError()), 'INFO');
			return $this->error("调用腾讯云接口错误：".$service->getError()->getMessage());
		}else{
			$channelSet = $a['channelSet'];
			$result = array();
			if($channelSet && !empty($channelSet)){
				foreach($channelSet as $key=>$value){
					if($value['channel_status'] != '1'){
						//无输入流
						$channel = $this->where(array('txy_channel_id'=>$value['channel_id'],'type'=>$is_fast))->find();
						if($channel){
							$channel['push_url'] = str_replace('MP4','mp4' , $channel['push_url']);
							if($is_fast == 1){
								//开始时间的前后三小时内 未被占用
								$ago_three   = date('Y-m-d H:i:s',strtotime($start_time) - 3*60*60); //三小时前
								$after_three = date('Y-m-d H:i:s',strtotime($start_time) + 3*60*60); //三小时后
								//查看 使用此频道的直播ID， 直播未开始 或直播中的时间是否 在前后三小时内
								$live_camera = M("vrlive.live_camera_angle", NULL);
								$list = $live_camera->where("signal_source =1 and source_id = ".$channel['channel_id']." and live.status not in(0,2,5) and start_time > '".$ago_three."' and start_time < '".$after_three."'")
									->join('vrlive.live on live.live_id = live_camera_angle.live_id','left')
									->select();
							}
							if($list){
								continue;
							}
							$result[] = $channel;
						}
					}
				}
			}

			return $result;
		}
	}
	
	public function getOperatorByChannelId($channelId){
		$list = $this->query("select b.real_name from vrlive.live_channel_operator a,vrlive.live_push_operator b where a.operator_id=b.operator_id and a.channel_id=$channelId");
		if($list && !empty($list)){
			$a = array();
			foreach($list as $key=>$value){
				$a[] = $value['real_name'];
			}
			return implode(' ', $a);
		}
		return '';
	}
	
	public function getOperatorListByChannelId($channelId){
		$list = $this->query("select a.operator_id,a.real_name,case when b.operator_id is null then 0 else 1 end as selected from vrlive.live_push_operator a left join vrlive.live_channel_operator b on (a.operator_id=b.operator_id and b.channel_id=$channelId) where a.status=1");
		
		return $list;
	}
}

